<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

$UID= util::encrypt($param['id']);
$fileset_path = TEMP_PATH."/".$UID;

if ($op=="cancel") {
	$_log->debug("Cancel upload $UID");
	deltree($fileset_path);
	$upload = new upload;
	$upload->id=$UID;
	$loader->delete($upload);
	$updata = new upload_data;
	$updata->objid=$upload->id;
	$loader->delete($updata);
	success();
} else if ($op=="ok") {
	$_log->debug("Confirm upload $UID");
	$upload = new upload;
	$upload->id=$UID;
	$r = $loader->get($upload);

	if ($r) {
		$new_filename = UPLOAD_PATH."/".$UID.".tgz";

		$updata = new upload_data;
		$updata->objid=$upload->id;
		$list = $loader->loadListBy($updata,$updata->listOK());
		$_log->debug("$UID: ".count($list)." valid items.");							

		$cmt = $param['comment'];
		if (strlen($param['comment'])>255) {
			$cmt = substr($param['comment'],0,255);
		}			

		if (count($list)>0) {
			$file_to_add = listFiles($fileset_path);
			$_log->debug($file_to_add);	
			
			if (count($file_to_add)>0) {
				$tar = new Archive_Tar($new_filename,"gz");
				$tar->createModify($file_to_add,"",$fileset_path);	
				unset($tar);
				@chmod($new_filename,0666);
				$ufiles = array();
				foreach ($list as $ud) {
					$f = new files($upload->projectid);
					$f->id=$ud->fileid;
					$loader->get($f);
					$f->statusid=FILE_STATUS_CMPND;
					$f->comment=$cmt;
					//$f->comment=String::format(s("Uploaded file {0} by {1}"),array($UID,$CURRENT_USER->userid));
					$loader->update($f);
					
					$his = new history($upload->projectid);
					$his->statusid=$f->statusid;
					$his->fileid=$f->id;
					$his->userid=$CURRENT_USER->userid;
					$his->comment=$f->comment;
					$his->action=HIS_ACT_UPD;	
					$loader->save($his);
					
					array_push($ufiles,$f);				
				}
	
				$upload->statusid=UPLOAD_OK;
				$upload->comment=$cmt;
				$loader->update($upload);	 
				$updata = new upload_data;
				$updata->objid=$upload->id;
				$loader->execute($updata->deleteFailed());
	
				deltree($fileset_path);

				mailNewFileUpload($CURRENT_USER,$ufiles,$new_filename,$cmt);
				success();
			} else {
				$_log->warn("File list return 0 over ".count($list)." [$UID].");
				failed(String::format(
					s($GEN_ERR_STR)
					,array($TRANSID)));				
			}
		} else {
			$list = $loader->loadList($updata);
			$f = jsonList($list,array("id","name","path","statusid","msg"));
			$_log->warn("No valid files [$UID] ($f).");
			failed(String::format(
				s("There are no valid files to confirm. If you think this is incorrect contact the administrator with the following transaction id {0}")
				,array($TRANSID)));
		}
	} else {
		$_log->warn("File not found [$UID].");
		failed(String::format(
			s($GEN_ERR_STR)
			,array($TRANSID)));
	}

} else {
	$upload = new upload;
	$upload->id=$UID;
	$r = $loader->get($upload);
	if ($r) {
		$updata = new upload_data;
		$updata->objid=$upload->id;
		$list = $loader->loadList($updata);
		$_log->debug("Files count: ". count($list));
		
		$vc = 0;
		foreach ($list as $ob) {
			if ($ob->statusid==FILE_UPLOAD_OK) $vc++;
		}
		$_log->debug("Valid files count: ". count($vc));
		out(jsonList($list,array("id","name","path","statusid","msg"),null,$vc));
	} else {
		$_log->warn("File not found [$UID].");
		out(jsonList(array(),null,0));
	}	
}

?>
